﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace project1
{
    class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> test1 = new GenericList<int>();
            test1.Add(1);
            test1.Add(2);
            Action<int> print = (s => Console.WriteLine(s));
            int sum = 0;
            test1.forEach(print);
            test1.forEach( m => sum = sum + m);
            Console.WriteLine("sum = {0}", sum);
            double average = 0;
            test1.forEach(m => average = (average + m) / 2);
            Console.WriteLine("average = {0}", average);
            int max = 0;
            test1.forEach(m => max = max > m ? max : m);
            Console.WriteLine("max = {0}", max);
            int min = 999999999;
            test1.forEach(m => min = min > m ? m : min);
            Console.WriteLine("min = {0}",min);
            Console.ReadKey();
        }
    }

    public class Node<T> 
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }
        public Node(T t)//Node的构造函数
        {
            Next = null;
            Data = t;
        }
    }

    public class GenericList<T>
    {
        public Node<T> head;
        public Node<T> tail;

        public GenericList()//构造函数
        {
            tail = head = null;
        }

        public Node<T> getHead()
        {
            return head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                tail = head = n;
            }
            else
            {
                tail.Next = n;
                tail = tail.Next;
            }
        }
        public void forEach(Action<T> action)
        {
            Node<T> temp = this.head;
            while (temp != null)
            {
                action(temp.Data);
                temp = temp.Next;
            }
        }
    }


}
